package com.cloud.Service.Impl;

import com.cloud.Service.MessageService;
import com.cloud.Service.UserService;
import com.cloud.dao.MessageDao;
import com.cloud.dto.MessageDto;
import com.cloud.dto.UserDto;
import com.cloud.entity.Message;
import com.cloud.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.sun.org.apache.xml.internal.serializer.utils.Utils.messages;


@Service
@Transactional
public class MessageImpl implements MessageService {
    @Autowired
    MessageDao messageDao;

    @Autowired
    UserService userService;


    /**
     * 发送邀请
     */
    @Override
    public MessageDto sendMessage(Integer received_id, UserDto user) {
        System.out.println(user);
        Message message = new Message();
        Date date1 = new Date();
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = d.format(date1);

        message.setSendId(user.getUserId());
        message.setReceiveId(received_id);
        message.setType(1);
        message.setInName(user.getName());
        String user_name = userService.getUserById(received_id).getName();
        message.setBeinName(user_name);
        message.setMInfo(user_name + ",您好！我是第" + user.getTeamId() + "团队的负责人，现诚挚地邀请您加入我们的团队，非常期待与您合作！");
        message.setMTime(date);
        message.setTeamId(user.getTeamId());
        message.setMStatus(0);

        MessageDto dto = new MessageDto();
        messageDao.save(message);
        BeanUtils.copyProperties(message, dto);
        return dto;
    }

    /**
     * 接受邀请
     */
    @Override
    public String update(Message message) {
        message.setMStatus(1);
        messageDao.save(message);
        return "将消息设置为已读";
    }

    @Override
    public List<MessageDto> getall() {
        List<Message> messages = messageDao.findAll();
        List<MessageDto> dtos = e2d(messages);
        return dtos;
    }

    @Override
    public List<MessageDto> unread(Integer userid) {
        List<Message> messages = messageDao.findByMStatusAndReceiveId(0, userid);
        List<MessageDto> dtos = e2d(messages);
        return dtos;
    }


    @Override
    public List<MessageDto> histroy(Integer userid) {
        List<Message> messages = messageDao.findByMStatusAndReceiveId(1, userid);
        List<MessageDto> dtos = e2d(messages);
        return dtos;
    }

    @Override
    public MessageDto getMessageById(Integer id) {
        Message message = messageDao.getOne(id);
        MessageDto dto = new MessageDto();
        if (message != null) {
            BeanUtils.copyProperties(message, dto);
        }
        return dto;
    }


    private MessageDto e2d(Message message) {
        if (message == null) {
            return null;
        }
        MessageDto messagedto = new MessageDto();
        BeanUtils.copyProperties(message, messagedto);
        return messagedto;
    }

    private List<MessageDto> e2d(List<Message> messages) {
        List<MessageDto> messagedtos = new ArrayList<>();
        if (messages != null && messages.size() > 0) {
            for (Message m : messages) {
                messagedtos.add(e2d(m));
            }
        }
        return messagedtos;
    }
}
